package com.unohacha.qqj.fzdr.personal.view.activity;

import android.support.design.widget.TabLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.common.bean.HttpResult;
import com.unohacha.qqj.fzdr.common.bean.ReleaseBean;
import com.unohacha.qqj.fzdr.common.bean.ThreeLevelBean;
import com.unohacha.qqj.fzdr.common.bean.TwoLevelBean;
import com.unohacha.qqj.fzdr.common.http.RetrofitManager;
import com.unohacha.qqj.fzdr.personal.adapter.ReleaseAdapter;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 浏览管理
 */
public class BrowsingManagementActivity extends BaseActivity {

    private final static String TAG = "BrowsingManagement";

    @BindView(R.id.common_back)
    ImageView commonBack;
    @BindView(R.id.common_title)
    RadioButton commonTitle;
    @BindView(R.id.common_title2)
    RadioButton commonTitle2;
    @BindView(R.id.common_right_image)
    ImageView rightImage;

    @BindView(R.id.rc_browsing_management)
    RecyclerView rcBrowsingManagement;

    @OnClick(R.id.common_title)
    void onCommonTitleClick() {
        firstLevel = "1";
    }

    @OnClick(R.id.common_title2)
    void onCommonTitle2Click() {
        firstLevel = "2";
    }

    // 保存供应/需求状态 1：供应 2：需求
    private String firstLevel = "1";
    // 二级标题
    @BindView(R.id.tb_two_level)
    TabLayout tbTwoLevel;

    private List<TwoLevelBean> levelBeans;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_browsing_management;
    }

    @OnClick(R.id.common_back)
    void onBackClick() {
        finish();
    }

    @OnClick(R.id.iv_level_more)
    void onLevelMoreClick() {
        int selected = tbTwoLevel.getSelectedTabPosition();
        String moreLevel = "";
        List<ThreeLevelBean> threeLevelBeans = levelBeans.get(selected).getTwo();
        for (ThreeLevelBean threeLevelBean : threeLevelBeans) {
            moreLevel += threeLevelBean.getName() + "/";
        }
        Log.d(TAG, "onLevelMoreClick: " + moreLevel);
        Toast.makeText(getApplicationContext(), moreLevel, Toast.LENGTH_SHORT).show();
        // 显示三级标题

    }

    @Override
    protected void initView() {
//        super.initView();
        commonTitle.setText("供应浏览");
        commonTitle2.setText("需求浏览");
        rightImage.setVisibility(View.INVISIBLE);

        // 加载二级标题
        initTwoLevel();
    }

    private void initTwoLevel() {
        showLoading();
        // 默认加载
        Call<HttpResult<List<TwoLevelBean>>> call = RetrofitManager.getInstance().supplyCate("2");
        call.enqueue(new Callback<HttpResult<List<TwoLevelBean>>>() {
            @Override
            public void onResponse(Call<HttpResult<List<TwoLevelBean>>> call, Response<HttpResult<List<TwoLevelBean>>> response) {
//                String data = response.body().getData().toString();
                List<TwoLevelBean> data = response.body().getData();
                if (data != null) {
                    upDataTwoLevel(data);
                } else {
                    Toast.makeText(getApplicationContext(), "获取失败", Toast.LENGTH_SHORT).show();
                }
                hideLoading();
            }

            @Override
            public void onFailure(Call<HttpResult<List<TwoLevelBean>>> call, Throwable t) {
                hideLoading();
            }
        });
    }

    private void upDataTwoLevel(List<TwoLevelBean> data) {
        Log.d(TAG, "upDataTwoLevel: " + data.size());
        // 保存标题
        if (levelBeans == null) {
            levelBeans = new ArrayList<>();
        }
        levelBeans.clear();
        levelBeans.addAll(data);
        for (TwoLevelBean twoLevelBean : data) {
            tbTwoLevel.addTab(tbTwoLevel.newTab().setText(twoLevelBean.getName()));
        }
        // 加载标题完毕，获取对应数据
//        tbTwoLevel.;
    }

    public void upDataRelease(ReleaseBean data) {
        Log.d(TAG, "upDataRelease: " + data);
        Toast.makeText(getApplicationContext(), data.getTotal(), Toast.LENGTH_SHORT).show();
//        rcReleaseManagement.setAdapter(new ReleaseAdapter(R.layout.item_release, data.getData()));

        //创建布局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rcBrowsingManagement.setLayoutManager(layoutManager);

        //创建适配器
        ReleaseAdapter adapter = new ReleaseAdapter(R.layout.item_release, data.getData());

        //给RecyclerView设置适配器
        rcBrowsingManagement.setAdapter(adapter);

        hideLoading();
    }

}
